<template>
  <div>
    <el-card>
      <el-button
        type="primary"
        class="add"
        :icon="Plus"
        :disabled="!ids.id3"
        @click="addAttr"
        >添加属性</el-button
      >
      <el-table :data="tableData.data" border style="width: 100%">
        <el-table-column label="序号" width="70" type="index" align="center" />
        <el-table-column prop="attrName" label="属性名称" width="180" />
        <el-table-column label="属性值列表" width="800">
          <template v-slot="{ row }">
            <el-tag
              class="tag"
              type="success"
              v-for="item in row.attrValueList"
              :key="item.id"
              >{{ item.valueName }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="操作" label="操作">
          <template v-slot="{ row }">
            <el-button
              type="primary"
              size="small"
              :icon="Edit"
              @click="handleClick(row)"
            ></el-button>
            <el-button
              @click="deleteAttrItem(row)"
              type="danger"
              size="small"
              class="delete"
              :icon="Delete"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script lang="ts">
export default {
  name: "XAttrList",
};
</script>

<script lang="ts" setup>
import { Delete, Edit, Plus } from "@element-plus/icons-vue";
import { onUpdated, reactive, onMounted } from "vue";
import type { productType } from "@/api/product/model";
import { reqProducType, reqDeleteAttr } from "@/api/product/attr";
import { useAttrStroe } from "@/stores/attr";
import { ElMessage } from "element-plus";
const store = useAttrStroe();
interface idType {
  ids: {
    id1: number | undefined;
    id2: number | undefined;
    id3: number | undefined;
  };
}
const props = defineProps<idType>();
interface dataType {
  data: productType[];
}
const emit = defineEmits(["toggle"]);

const tableData = reactive<dataType>({
  data: [
    {
      id: undefined,
      attrName: "",
      categoryId: undefined,
      categoryLevel: undefined,
      attrValueList: [
        {
          id: undefined,
          valueName: "",
          attrId: undefined,
        },
      ],
    },
  ],
});
async function request() {
  if (!props.ids.id3) return;
  const result = await reqProducType({
    category1Id: props.ids.id1 as number,
    category2Id: props.ids.id2 as number,
    category3Id: props.ids.id3 as number,
  });
  console.log(result, "@@@@");
  tableData.data = result as any;
}
onMounted(request);
onUpdated(request);
const handleClick = (row: any) => {
  store.$state.isEditItem = true;
  store.$state.attrRow = row;
  emit("toggle", true);
};
async function deleteAttrItem(row: any) {
  await reqDeleteAttr(row.id);
  ElMessage.success("删除品牌成功!!");
  request();
}
function addAttr() {
  emit("toggle", true);
  store.$state.isEditItem = false;
}
</script>

<style scoped>
.delete {
  margin-left: 20px;
}
.add {
  margin-bottom: 10px;
}
.tag {
  margin-right: 10px;
}
</style>
